package com.softserveinc.data.help;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.util.Log;

import com.softserveinc.data.help.MovieDBOpenHelper.MovieDetailsTableMeta;
import com.softserveinc.data.help.MovieDBOpenHelper.MovieImagesTableMeta;
import com.softserveinc.model.Certification;
import com.softserveinc.model.Movie;
import com.softserveinc.model.MovieDetails;

public class DBDataProcessor {

	private static final String TAG = DBDataProcessor.class.getSimpleName();

	public DBDataProcessor() {
	}

	public List<Movie> parseMovieListCursor(final Cursor cursor) {
		if (cursor != null && cursor.moveToFirst()) {
			List<Movie> movies = new ArrayList<Movie>();

			do {
				Movie movie = new Movie();

				movie.setTitle(cursor.getString(cursor
						.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_TITLE)));
				movie.setPopularity(cursor.getDouble(cursor
						.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_POPULARITY)));

				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

				try {
					movie.setReleaseDate(format.parse(cursor.getString(cursor
							.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_RELEASE_DATE))));
				} catch (Exception e) {
					movie.setReleaseDate(null);
				}
				movie.setPosterHttpUrl(cursor.getString(cursor
						.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_POSTER_URL)));
				movie.setPosterLocalUrl(cursor.getString(cursor
						.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_POSTER_LOCAL_URL)));
				movie.setCertification(Certification.getCertificationForName(cursor.getString(cursor
						.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_CERTIFICATION_NAME))));
				movie.setId(cursor.getInt(cursor
						.getColumnIndex(MovieDBOpenHelper.MoviesTableMeta.COLUMN_NAME_TMDB_ID)));

				movies.add(movie);
			} while (cursor.moveToNext());

			cursor.close();
			Log.d(TAG, "is closed " + cursor.isClosed());

			return movies;
		}

		return null;
	}

	public MovieDetails parseMovieDetailsCursor(final Cursor cursor) {
		Log.d(TAG, "parser called");

		if (cursor != null && cursor.moveToFirst()) {
			Log.d(TAG, "parser in action");
			MovieDetails movieDetails = new MovieDetails();

			movieDetails.setCast(cursor.getString(cursor
					.getColumnIndex(MovieDetailsTableMeta.COLUMN_NAME_CAST)));
			movieDetails
					.setOverview(cursor.getString(cursor
							.getColumnIndex(MovieDetailsTableMeta.COLUMN_NAME_OVERVIEW)));
			movieDetails
					.setTrailerUrl(cursor.getString(cursor
							.getColumnIndex(MovieDetailsTableMeta.COLUMN_NAME_MOVIE_TRAILER_URL)));

			cursor.close();

			return movieDetails;
		}

		return null;
	}

	public void addImagesToMovieDetails(final Cursor cursor,
			final MovieDetails movieDetails) {
		Log.d(TAG, "images loading");

		if (cursor != null && cursor.moveToFirst()) {
			List<String> thumbHttpUrls = new ArrayList<String>();
			List<String> thumbLocalUrls = new ArrayList<String>();
			List<String> thumbHttpUrlsBig = new ArrayList<String>();

			Log.d(TAG, "loading started");

			do {
				Log.w(TAG, "image loading");

				thumbHttpUrls
						.add(cursor.getString(cursor
								.getColumnIndex(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP)));

				thumbLocalUrls
						.add(cursor.getString(cursor
								.getColumnIndex(MovieImagesTableMeta.COLUMN_NAME_THUMB_LOCAL)));

				thumbHttpUrlsBig
						.add(cursor.getString(cursor
								.getColumnIndex(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP_BIG)));
			} while (cursor.moveToNext());

			cursor.close();

			movieDetails.setThumbHttpUrls(thumbHttpUrls);
			movieDetails.setThumbLocalUrls(thumbLocalUrls);
			movieDetails.setThumbHttpUrlsBig(thumbHttpUrlsBig);

			Log.d(TAG, "loading ended");
		}
	}
}
